import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/view/loading_view.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/model/friend_user_info_model.dart';
import 'package:love_newf/app/page/chat/chat_view.dart';
import 'package:love_newf/value/app_const.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart';

import 'conversation_list_logic.dart';

class ConversationListPage extends StatefulWidget {
  const ConversationListPage({Key? key}) : super(key: key);

  @override
  State<ConversationListPage> createState() => _ConversationListPageState();
}

class _ConversationListPageState extends State<ConversationListPage> {
  final ConversationListLogic logic = Get.find<ConversationListLogic>();

  @override
  Widget build(BuildContext context) {
    return LContainer(
            child: GetBuilder<ConversationListLogic>(builder: (L) => body))
        .bgColor(Colors.white);
  }

  get body => LoadingView(
        datas: logic.datas,
        itemCall: (context, position) => itemView(position),
        isPullUp: true,
        isPullDown: true,
        onStateClick: (type) {
          if ((logic.datas?.length ?? 0) == 0) logic.datas = null;
          logic.update();
        },
        onLoad: (type) async {
          if (type == 1) {
            logic.nextSeq = "0";
          } else if (type == 2) {
            // logic.pageNum++;
          }
          return await logic.getData();
        },
        onSuccess: () {
          if (logic.datas != null) logic.update();
        },
      );

  itemView(int position) {
    V2TimConversation item = logic.datas![position]!;
    return LRow.eazy([
      LImage.easy(item.faceUrl, 50.w, 50.w, 50.w),
      8.lsw,
      LColumn.eazy([
        LText.easy(item.showName, 14.sp, fw5),
        12.lsh,
        LText.easy(item.lastMessage?.textElem?.text ?? "", 12.sp, fw4,
                const Color(0xFFAAAAA8))
            .maxLines(1),
      ]).align(Alignment.topLeft).flex(1),
      LColumn.eazy([
        LText.easy(item.draftTimestamp, 12.sp, fw4, const Color(0xFFAAAAA8)),
        15.lsh,
        if (item.unreadCount != 0)
          LText.easy(item.unreadCount, 11.sp, fw4, Colors.white)
              .paddingH(7.w)
              .radius(10.w)
              .bgColor(Color(0xFFFF6162))
              .center(),
      ]),
    ]).padding(left: 12.w, right: 12.w, top: 15.w, bottom: 15.w).onTapL(() {
      FriendUserInfoModel userInfoModel = FriendUserInfoModel();
      userInfoModel.userId = int.parse(item.userID!);
      userInfoModel.nick = item.showName!;
      userInfoModel.avatar = item.faceUrl!;

      ChatPage.openPage(item.userID, friendUserInfo: userInfoModel);
    });
  }
}
